package com.atguigu.schedule.controller;

import com.atguigu.schedule.pojo.SysUser;
import com.atguigu.schedule.protocol.CommonResponse;
import com.atguigu.schedule.protocol.ResponseCodeEnum;
import com.atguigu.schedule.service.Impl.SysUserServiceImpl;
import com.atguigu.schedule.service.SysUserService;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.xdevapi.JsonString;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/user/*")
public class SysUserController extends BaseController<SysUserController> {

    private SysUserService userService = new SysUserServiceImpl();

    protected void checkUsernameUsed(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        boolean isUsed = userService.checkUsername(username);
        CommonResponse<String> data = new CommonResponse();
        if (isUsed) {
            data.setCode(ResponseCodeEnum.NAME_OCCUPIED_ERROR.getCode());
        } else {
            data.setCode(ResponseCodeEnum.SUCCESS.getCode());
        }
        resp.setContentType("application/json,charset=utf-8");
        String info = (new ObjectMapper()).writeValueAsString(data);
        resp.getWriter().write(info);
    }

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收客户端提交的参数
        String username = req.getParameter("username");
        String password = req.getParameter("userPwd");
        SysUser sysUser = new SysUser(0, username, password);
        // 调用service层方法,完成登录
        int code = userService.login(sysUser);
        // 根据登录结果,做页面跳转
        if (code == 0) {
            // 保留一个session域对象
            HttpSession session = req.getSession();
            session.setAttribute("sysUser", sysUser);
            // 再去跳转页面
            resp.sendRedirect("/showSchedule.html");
        } else if (code == 101) {
            resp.sendRedirect("/loginUsernameError.html");
        } else if (code == 102) {
            resp.sendRedirect("/loginUserPwdError.html");
        }
    }

    public void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收客户端提交的参数
        String username = req.getParameter("username");
        String password = req.getParameter("userPwd");
        SysUser sysUser = new SysUser(0, username, password);
        // 调用service层方法,完成注册
        int code = userService.register(sysUser);
        // 根据注册结果, 做页面跳转
        if (code == 0) {
            resp.sendRedirect("/registSuccess.html");
        } else {
            resp.sendRedirect("/registFail.html");
        }
    }
}
